package br.com.vti.leilao.model.entity;

import br.com.vti.leilao.model.annotation.Constraint;
import br.com.vti.leilao.model.annotation.Constraints;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 *
 * @author ednardomatos
 */
@Entity
@Table(name = "t_usuario", catalog = "homologacao", schema = "leilao")
@SequenceGenerator(name = "seq_usuario", sequenceName = "leilao.seq_usuario", initialValue = 1, allocationSize = 1)
@Constraints({
    @Constraint(name = "t_usuario_email_key", message = {"erro.existe.registro", "campo.email"})})
public class Usuario implements Model<Integer>, UserDetails {

    private static final long serialVersionUID = -4689561736858315060L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_usuario")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 80)
    @Column(name = "nome")
    private String nome;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 120)
    @Column(name = "email", updatable = false)
    private String email;

    @Basic(optional = false)
    @Size(min = 1, max = 60)
    @Column(name = "senha")
    private String senha;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "nm_setor")
    private String nmSetor;

    @JoinColumn(name = "empresa_fk", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Empresa empresaFk;

    @Size(min = 1, max = 40)
    @Column(name = "permissoes")
    private String permissoes;

    @Column(name = "status")
    private Character status;

    public Usuario() {
    }

    public Usuario(String nome, String email, String senha, String nmSetor) {
        this.nome = nome;
        this.email = email;
        this.senha = senha;
        this.nmSetor = nmSetor;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getNmSetor() {
        return nmSetor;
    }

    public void setNmSetor(String nmSetor) {
        this.nmSetor = nmSetor;
    }

    public Empresa getEmpresaFk() {
        return empresaFk;
    }

    public void setEmpresaFk(Empresa empresaFk) {
        this.empresaFk = empresaFk;
    }

    public String getPermissoes() {
        return permissoes;
    }

    public void setPermissoes(String permissoes) {
        this.permissoes = permissoes;
    }

    public Character getStatus() {
        return status;
    }

    public void setStatus(Character status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 43 * hash + (this.nome != null ? this.nome.hashCode() : 0);
        hash = 43 * hash + (this.nmSetor != null ? this.nmSetor.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Usuario other = (Usuario) obj;
        if ((this.nome == null) ? (other.getNome() != null) : !this.nome.equals(other.getNome())) {
            return false;
        }
        if ((this.nmSetor == null) ? (other.getNmSetor() != null) : !this.nmSetor.equals(other.getNmSetor())) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Usuario{" + "nome=" + nome + ", email=" + email + ", nmSetor=" + nmSetor + ", empresa=" + empresaFk + ", permissoes=" + permissoes + ", status=" + status + "}";
    }

    @Override
    @Transient
    public List<GrantedAuthority> getAuthorities() {
        String[] arrayPermissoes = getPermissoes().split(";");
        List<GrantedAuthority> listaPermissoes = new ArrayList<GrantedAuthority>();
        for (String permissao : arrayPermissoes) {
            listaPermissoes.add(new SimpleGrantedAuthority(permissao));
        }
        return listaPermissoes;
    }

    @Override
    @Transient
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @Transient
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @Transient
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @Transient
    public boolean isEnabled() {
        return getStatus().equals(new Character('A'));
    }

    @Override
    public String getPassword() {
        return getSenha();
    }

    @Override
    public String getUsername() {
        return getEmail();
    }

}
